<template>
	<view>
		<view :class="item.is_choose ? 'list-active list' : 'list'" v-for="item in list" @click="choose_address(item.id)">
			<view class="list-top">
				<view class="list-top-text"> 
					<text class="list-top-t">{{ item.provinceName }}{{ item.cityName }}{{ item.countyName }}</text>  
					<br>
					{{ item.address }}
				</view>	
				<view class="list-top-img" @click.stop="edit_address(item)">
					<image src="https://wechat-pictures-1301970825.file.myqcloud.com/shop/hyda-edit.png" mode=""></image>
				</view>
			</view>
			
			<view class="list-bottom">
				<view class="list-bottom-name">{{ item.name }}</view>
				<view class="list-bottom-phone">{{ item.phone }}</view>
				<view class="list-bottom-label" v-if="item.isDefault">默认</view>
				<view v-if="item.tagName" class="list-bottom-label">{{ item.tagName }}</view>
			</view>
		</view>
		<no_data title="暂无数据"  v-show="!list[0]"></no_data>
		<view style="height: 200upx;"></view>
		
		
		<view class="btm-btn">
			<view class="add-address" @click="add_address()"><text>+</text>新增地址</view>
			<view v-if="is_show" class="shiyong" @click="is_shiyong()">确定使用</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				buyUserid:'',
				list:[],
				current_choose_address_id:0,
				orderNo:'',
				groupNo:'',
				
				is_show:false,
			}
		},
		onShow() {
			var that = this;
			setTimeout(function() {
				that.getAddressList();
			},300)
		},
		onLoad(req) {
			if(req.orderNo) {
				this.orderNo = req.orderNo
				this.is_show = true;
			}
			if(req.groupNo) {
				this.groupNo = req.groupNo
				this.is_show = true;
			}
			// this.getAddressList();
		},
		methods: {
			// 获取地址列表
			getAddressList() {
				var data = {
					unionId:this.buyUserid,
				}
				this.$javaApi.doRequest("post","buying/group/customer/getAddressListByUnionId",data).then(res=>{
					if(res.data.code == 0) {
						for(var i = 0; i < res.data.data.length; i++ ) {
							res.data.data[i].is_choose = false;
						}
						// 默认选中第一个
						this.current_choose_address_id = res.data.data[0].id;
						this.list = res.data.data;
						console.log(this.list)
					}else {
						uni.showToast({
							icon:'none',
							title:res.data.message,
						})
					}
				}) 
			},
			choose_address(id) {
				for(var i = 0; i < this.list.length; i++ ) {
					if(this.list[i].id == id) {
						if(this.list[i].is_choose) {
							this.list[i].is_choose = false;
						}else {
							this.list[i].is_choose = true;
						}
					}else {
						this.list[i].is_choose = false;
					}
				}
				this.current_choose_address_id = id;
			},
			is_shiyong() {
				// 判读是在团购详情修改还是在支付页修改
				if(this.groupNo != ''){
					uni.reLaunch({
						 url: '/pages/userIndex/groupInfo?groupNo=' + this.groupNo + '&addressId=' + this.current_choose_address_id
					});
					return
				}
				var data = {
					addressId:this.current_choose_address_id,
					orderNo:this.current_orderNo,
				}
				this.$javaApi.doRequest("post","buying/groupfront/addressOrder/saveOrderAddress",data).then(res=>{
					if(res.data.code == 0) {
						uni.showToast({
							icon:'none',
							title:res.data.message,
						})
						setTimeout(function() {
							uni.navigateBack()
						},1500)
					}else {
						uni.showToast({
							icon:'none',
							title:res.data.message,
						})
					}
				}) 
			},
			add_address() {
				uni.navigateTo({
					url:'/pages/userMy/address/add'
				})
			},
			edit_address(info) {
				uni.navigateTo({
					url:'/pages/userMy/address/edit?info='+encodeURIComponent(JSON.stringify(info))
				})
			},
			
		}
	}
</script>

<style scoped>
.list {
	width: 650upx;
	min-height: 50upx;
	padding: 20upx;
	margin: 0 auto;
	margin-top: 30upx;
	background: #FFFFFF;
	box-shadow: 0upx 4upx 12upx 0upx rgba(0,0,0,0.25);
	border-radius: 24upx;
	/* display: flex; */
	/* box-sizing: border-box; */
}
.list-active {
	border:3upx solid #3089E7;
}
.list-top {
	min-height: 10upx;
	width: 650upx;
	border-bottom: 2upx dashed #ccc;
	display: flex;
}
.list-top-t {
	color: #333;
	font-size: 32upx;
	font-weight: 500;
}
.list-top-text {
	width: 500upx;
	height: 80upx;
	margin:30upx 0upx;
	margin-top: 10upx;
	font-size: 400;
	font-size: 32upx;
	overflow:hidden;
	text-overflow:ellipsis;
	display:-webkit-box;
	-webkit-line-clamp:2;
	-webkit-box-orient:vertical;
}
.list-top-img {
	flex: 1;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
}
.list-top image {
	width: 48upx;
	height: 46upx;
	margin-left: 20upx;
}
.list-bottom {
	height: 50upx;
	width: 650upx;
	line-height: 50upx;
	font-size: 28upx;
	color: #333;
	display: flex;
	margin-top: 20upx;
}
.list-bottom-name {
	font-weight: 500;
}
.list-bottom-phone {
	font-weight: 400;
	margin-left: 30upx;
	margin-right: 30upx;
}
.list-bottom-label {
	min-width: 10upx;
	height: 40upx;
	line-height: 40upx;
	color: #fff;
	padding: 0 15upx;
	background-color: #FF7E3E;
	margin-top: 5upx;
	margin-right: 30upx;
	font-size: 24upx;
	border-radius: 4upx;
}

.btm-btn {
	width: 690upx;
	height: 100upx;
	line-height: 100upx;
	text-align: center;
	color: #fff;
	font-size: 32upx;
	position: fixed;
	bottom: 50upx;
	left: 30upx;
	display: flex;
}
.add-address {
	flex: 4;
	background-color: #FF7E3E;
	border-radius: 12upx;
}
.add-address text {
	margin-right: 20upx;
}
.shiyong {
	flex: 6;
	margin-left: 30upx;
	background-color: #FF7E3E;
	border-radius: 12upx;
}
</style>
